/*
 * CPU6502.h
 *
 *  Created on: 2011-2-25
 *      Author: Dallas Cao
 */

#ifndef CPU6502_H_
#define CPU6502_H_

#ifndef TYPEDEF_H_
#include "../Typedef.h"
#endif

#include "APU.h"
#include "MMC.h"
#include "PPU.h"
#include "ROM.h"

// 6502 flags for status register
#define		C_FLAG		0x01		// 1: Carry
#define		Z_FLAG		0x02		// 1: Zero
#define		I_FLAG		0x04		// 1: Irq disabled
#define		D_FLAG		0x08		// 1: Decimal mode flag (NES unused)
#define		B_FLAG		0x10		// 1: Break
#define		R_FLAG		0x20		// 1: Reserved (Always 1)
#define		V_FLAG		0x40		// 1: Overflow
#define		N_FLAG		0x80		// 1: Negative

#define 	NMI_VECTOR	0xFFFA
#define 	RES_VECTOR	0xFFFC
#define 	IRQ_VECTOR	0xFFFE

// 6502 CPU Registers
typedef struct
{
	// the Program Counter
	WORD PC;

	// the Accumulator
	BYTE A;

	// the Stack Pointer
	BYTE P;

	// the X Index Register
	BYTE X;

	// the Y Index Register
	BYTE Y;

	// the Status Register
	// Bit No. 7   6   5   4   3   2   1   0
	// 		   N   V   R   B   D   I   Z   C
	BYTE S;

	BYTE INT_pending;
}R6502;

class CPU6502
{
public:
	CPU6502();
	virtual ~CPU6502();

	BYTE 	Read(WORD addr);
	WORD 	ReadW(WORD addr);
	void 	Write(WORD addr, BYTE val);
	void 	Reset();
	INT 	Exec(INT CpuCycle);
	BOOL	isRunning()
	{
		return m_bIsRunning;
	}

protected:
	R6502	R;
	BOOL	m_bIsRunning;
};

#endif /* CPU6502_H_ */
